package com.bjpowernode.web.controller;

import com.bjpowernode.api.dto.RPCResult;
import com.bjpowernode.api.model.User;
import com.bjpowernode.constants.Constant;
import com.bjpowernode.constants.RedisKey;
import com.bjpowernode.constants.enums.Code;
import com.bjpowernode.web.resp.ResponseObj;
import com.bjpowernode.web.resp.view.RankView;
import io.swagger.annotations.*;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author 麦翔盛
 * @ClassName: InvestController
 * @Package: com.bjpowernode.web.controller
 * @description: 投资接口
 * @date 2022/5/19 18:37
 */
@Api(tags = "投资接口")
@RestController
public class InvestController extends BaseController {

    /**
     * 获取投资排行榜前三的用户
     *
     * @return 获取投资排行榜前三的用户
     */
    @ApiOperation(value = "获取投资排行榜前三的用户", notes = "获取投资排行榜前三的用户")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功", response = ResponseObj.class),
            @ApiResponse(code = 500, message = "服务器内部异常", response = String.class)
    })
    @ApiImplicitParams({

    })
    @GetMapping("/v1/invest/rank")
    public ResponseObj investRank() {
        //int i = 1/0;
        //String a = null;
        //a.length();
        //绑定zset
        BoundZSetOperations<String, String> zSetOps =
                stringRedisTemplate.boundZSetOps(RedisKey.INVEST_RANK);
        //获取前三名的数据
        Set<ZSetOperations.TypedTuple<String>> sets =
                zSetOps.reverseRangeWithScores(0, 2);
        //创建成绩列表
        List<RankView> rankViewList = new ArrayList<>();
        if (sets != null) {
            // 将值取出放到列表中
            sets.forEach(data -> {
                rankViewList.add(new RankView(data.getValue(), data.getScore()));
            });
        }
        return ResponseObj.ok().content(rankViewList);

    }

    /**
     * 购买产品
     * @param uid 用户id
     * @param productId 产品id
     * @param money 购买金额
     */
    @ApiOperation("购买产品")
    @GetMapping("/v1/invest/product")
    public ResponseObj investProduct(@RequestHeader("uid") Integer uid,
                                     @RequestParam("productId") Integer productId,
                                     @RequestParam("money")BigDecimal money){

        //校验数据格式
        if (uid ==null || uid < 0 || productId == null || productId < 0 || money == null){
            return ResponseObj.error(Code.REQ_PARAM_FORMAT_ERR);
        }
        //校验购买金额格式
        if (money.doubleValue() < Constant.MIN_BID_MONEY || money.doubleValue() % Constant.MIN_BID_MONEY != 0){
            return ResponseObj.error(Code.REQ_PARAM_FORMAT_ERR);
        }

        //购买理财产品
        RPCResult rpcResult = investService.investProduct(uid, productId, money);

        //判断是否购买成功
        if (!rpcResult.isResult()){
            return ResponseObj.fail().message(rpcResult.getMessage());
        }

        //修改投资排行榜中的信息
        String rankKey = RedisKey.INVEST_RANK;
        BoundZSetOperations<String, String> opts
                = stringRedisTemplate.boundZSetOps(rankKey);
        //修改当前用户在排行榜中的投资金额
        User user = userService.queryUserById(uid);
        opts.incrementScore(user.getPhone(),money.doubleValue());

        return ResponseObj.ok();

    }
}
